from time import sleep

import logging
from appium.webdriver.common.touch_action import TouchAction
from appium.webdriver.webdriver import WebDriver
from appium.webdriver.common.mobileby import MobileBy
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait

from lagou_cekai.task3_3appium2.ten_class_Enterprise_wechat.page.page_element.el_mian_page import xiaoxi


class BasePage33:
    def __init__(self, driver: WebDriver=None):
        self.driver = driver

    # 查找元素
    def find(self,page,name,value):
        logging.info(f"查找元素{page}，{name}，{value}")

        return self.driver.find_element(*page[name][value])

    # 查找并点击元素
    def find_click(self,page,name,value):
        logging.info(f"点击元素{page}，{name}，{value}")
        self.find(page,name,value).click()

    # 查找元素并输入
    def find_sendkeys(self,page,name,value,data):
        """
        :param page: el中字典名称
        :param name: el中页面名称
        :param value: el中元素名称
        :param data: send_keys中需要输入的数据
        :return:
        """
        logging.info(f"查找元素{page}，{name}，{value}，并输入{data}")
        self.find(page,name,value).send_keys(data)

    # 获取元素的text属性
    def find_get_text(self,page,name,value):
        logging.info(f"获取元素{page}，{name}，{value}的text属性")
        return self.find(page,name,value).text

    # 获取toast元素的text属性
    def find_get_toast_text(self):
        logging.info("获取元素toast的text属性")
        return  self.driver.find_element(MobileBy.XPATH,"//*[@class='android.widget.Toast']").text

    # 滑动屏幕
    def huadong(self,X_initial_coordinates,Y_initial_coordinates,X_finall_coordinates,Y_finall_coordinates):
        """
        :param X_initial_coordinates: 滑动的初始坐标X坐标 ,此处填写为全屏X坐标的百分比
        :param Y_initial_coordinates: 滑动的初始坐标Y坐标,此处填写为全屏Y坐标的百分比
        :param X_finall_coordinates: 滑动的终止坐标X坐标,此处填写为全屏X坐标的百分比
        :param Y_finall_coordinates: 滑动的终止坐标Y坐标,此处填写为全屏Y坐标的百分比
        :return:
        """

        phone_width = self.driver.get_window_size()["width"]
        phone_height = self.driver.get_window_size()["height"]
        # 日志
        logging.info(f"活动屏幕，从屏幕的坐标[{phone_width}*{X_initial_coordinates},{phone_height}*{Y_initial_coordinates}]，\
        滑动到[{phone_width}*{X_finall_coordinates}，{phone_height}*{Y_finall_coordinates}]")

        action = TouchAction(self.driver)
        action.press(x=phone_width*X_initial_coordinates, y=phone_height*Y_initial_coordinates).wait(200).\
            move_to(x=phone_width*X_finall_coordinates,y=phone_height*Y_finall_coordinates).release().perform()

    # 滑动查找元素
    def huadong_find(self,page,name,value,huadong_number):
        logging.info(f"滑动{huadong_number}次，查找元素{page}，{name}，{value}")
        for i in range(huadong_number):
            try:
                a = self.find(page, name, value)
                if a != None:
                    print('找到了')
                    return a
            except:
                print('没有找到')
                self.huadong(0.5,0.75,0.5,0.25)

    # 滑动点击元素
    def huadong_find_click(self,page,name,value,huadong_number):
        logging.info(f"滑动{huadong_number}次，查找元素{page}，{name}，{value}并点击")
        a=self.huadong_find(page,name,value,huadong_number)
        # 自己写了一个显示等待，等到某元素属性为可点击，下面有一个官方封装好的，也是等到某元素属性为可点击
        def wait(x):
            return a.get_attribute('clickable')=='true'
        WebDriverWait(self.driver, 3).until(wait)
        # WebDriverWait(self.driver, 5).until(expected_conditions.element_to_be_clickable(*page[name][value]))
        a.click()

    # 点击弹出“确定”按钮
    def fram_click_queding(self):
        logging.info("点击弹框中的确定按钮")
        a=self.driver.find_element(MobileBy.XPATH, '//*[@text="确定"]')
        def wait2(x):
            return a!=None
        WebDriverWait(self.driver,3).until(wait2)
        a.click()

    def back_initial_page(self,num):
        logging.info("返回初始页面")
        for i in range(num):
            try:
                a=self.find(xiaoxi,"消息","消息")
                b=self.find(xiaoxi,"消息","通讯录")
                c=self.find(xiaoxi,"消息","工作台")
                d=self.find(xiaoxi,"消息","我")
                if a!=None and b!=None and c!=None and d!=None:
                    self.find_click(xiaoxi,"消息","消息")
            except:
                self.driver.back()





